package com.daiji.controller;

import com.alibaba.fastjson.JSON;
import com.daiji.dao.impl.StudentDaoImpl;
import com.daiji.entity.JsonEntity;
import com.daiji.entity.StudentEntity;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/stu/*")
public class StudentServlet extends HttpServlet {
    StudentDaoImpl studentDao = new StudentDaoImpl();

    /**
     * 根据不同的请求调用不同的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        int i = requestURI.lastIndexOf("/");
        String s = requestURI.substring(i+1);

        switch (s){
            case "selectStudents":
                selectStudents(req,resp);
                break;
            case ("insertStudent"):
                if ("null".equals(req.getParameter("id"))){
                    insertStudent(req,resp);
                }else {
                    updateStudent(req,resp);
                }

                break;
            case ("deleteStudent"):
                deleteStudent(req,resp);
                break;
            default:
                break;
        }
    }

    /**
     * 查询所有学生记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void selectStudents(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        StudentEntity student = new StudentEntity();
        PrintWriter writer = resp.getWriter();
        try {
            BeanUtils.populate(student,map);
            List<StudentEntity> list = studentDao.selectStudents(student);
            int count = studentDao.getCount(student);

            JsonEntity jsonEntity = new JsonEntity(count, list);
            String s = JSON.toJSONString(jsonEntity);
            writer.print(s);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }





        /*List<StudentEntity> list = studentDao.selectStudents();
        PrintWriter writer = resp.getWriter();

            JsonEntity jsonEntity = new JsonEntity(list.size(), list);
            String s = JSON.toJSONString(jsonEntity);
            writer.print(s);
            writer.close();*/
    }

    /**
     * 新增一条学生记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insertStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String[]> map = req.getParameterMap();
        System.out.println(map);

        StudentEntity student = new StudentEntity();
        try {
            BeanUtils.populate(student,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        int i = studentDao.insertStudent(student);

        if (i > 0){
            req.getRequestDispatcher("/showStudents.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","学生信息新增失败");
            req.getRequestDispatcher("/fail.jsp").forward(req,resp);
        }
    }

    /**
     * 修改学生信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{


        Map<String, String[]> map = req.getParameterMap();
        StudentEntity student = new StudentEntity();
        try {
            BeanUtils.populate(student,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int i = studentDao.updateStudent(student);

        if (i > 0){
            req.getRequestDispatcher("/showStudents.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","修改用户信息失败");
            req.getRequestDispatcher("/fail.jsp").forward(req,resp);
        }
    }

    /**
     * 删除学生记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String ids = req.getParameter("ids");
        String[] arrID = ids.split(",");
        System.out.println(ids);
        int i = 0;
        for (String s : arrID) {

            i = studentDao.deleteStudent(Integer.parseInt(s));
        }

        if (i > 0){
            req.getRequestDispatcher("/showStudents.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","删除记录失败");
            req.getRequestDispatcher("/fail.jsp").forward(req,resp);
        }
    }
}
